﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.INoticeB;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.NoticeContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class NoticeController : ControllerBase
    {
        private readonly INoticeBLL _noticeBll;
        public NoticeController(INoticeBLL noticeBll) { 
            _noticeBll = noticeBll;
        }
        /// <summary>
        /// 查询所有通知类别
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllNotice() {
            try
            {
                return _noticeBll.GetAllNotice();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllNotice服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据消息类型编号查询消息
        /// </summary>
        /// <param name="NoticeId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllNoticeByNoticeId(int NoticeId) {
            try
            {
                return _noticeBll.GetAllNoticeByNoticeId(NoticeId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllNoticeByNoticeId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 新增消息类别
        /// </summary>
        /// <param name="notice">消息类别</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertNotice(Notice notice) {
            try
            {
                return _noticeBll.InsertNotice(notice);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertNotice服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改消息类别
        /// </summary>
        /// <param name="data">消息类别信息</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateNotice(Notice data) {
            try
            {
                return _noticeBll.UpdateNotice(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateNotice服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
